<?php
namespace frontend\controllers;

use frontend\models\ArtForm;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\widgets\file_upload\FileUpload;
use common\models\member\Member;
use common\models\category\Category;
use common\models\articles\Articles;
use yii\data\Pagination;

class AboutController extends Controller
{     
	// public $layout = 'ucenter';	
	public function actions()
    {
        return [
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',     //这里扩展地址别写错
                'config' => [
                ]
            ],
			'ueditor'=>[
				'class' => 'common\widgets\ueditor\UeditorAction',
				'config'=>[
					//上传图片配置
					'imageUrlPrefix' => "", /* 图片访问路径前缀 */
					'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
				]
			]
        ];
    } 
     //添加文章
	 public function actionJianjie(){
	 	$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = $this->action->id;
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}
        $info = Articles::find()->where(['category_id'=>$categoryid])->asArray()->one();

		$title = $info['title'];
		$subhead = $info['subhead'];
		$intro = $info['intro'];
		$img = $info['img'];
		$link = $info['link'];
		$content = $info['content'];
		$seokeyword = $info['seokeyword'];
		$seocontent = $info['seocontent'];

		$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
        return $this->render('jianjie',[
            'title' => $title,
            'subhead' => $subhead,
            'intro' => $intro,
            'img' => $img,
            'link' => $link,
			'content' => $content,
            'seokeyword' => $seokeyword,
            'seocontent' => $seocontent,
            'child' => $child,
        ]);
    }
    //组织架构
	public function actionZzjg(){
	 	$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = $this->action->id;
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}
        $info = Articles::find()->where(['category_id'=>$categoryid])->asArray()->one();

		$title = $info['title'];
		$subhead = $info['subhead'];
		$intro = $info['intro'];
		$img = $info['img'];
		$link = $info['link'];
		$content = $info['content'];
		$seokeyword = $info['seokeyword'];
		$seocontent = $info['seocontent'];

		$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
        return $this->render('jianjie',[
            'title' => $title,
            'subhead' => $subhead,
            'intro' => $intro,
            'img' => $img,
            'link' => $link,
			'content' => $content,
            'seokeyword' => $seokeyword,
            'seocontent' => $seocontent,
            'child' => $child,
        ]);
   	}
	//领导简介
	public function actionLeaderintr(){
	 	$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = $this->action->id;
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}
        $info = Articles::find()->where(['category_id'=>$categoryid])->asArray()->one();

		$title = $info['title'];
		$subhead = $info['subhead'];
		$intro = $info['intro'];
		$img = $info['img'];
		$link = $info['link'];
		$content = $info['content'];
		$seokeyword = $info['seokeyword'];
		$seocontent = $info['seocontent'];

		$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
        return $this->render('jianjie',[
            'title' => $title,
            'subhead' => $subhead,
            'intro' => $intro,
            'img' => $img,
            'link' => $link,
			'content' => $content,
            'seokeyword' => $seokeyword,
            'seocontent' => $seocontent,
            'child' => $child,
        ]);
   	}

   	public function actionCommittee(){
	 	$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = $this->action->id;
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}

	 	$query = Articles::find()->where(['category_id'=>$categoryid]);
		$count = $query->count();
		$page = new Pagination(['totalCount' => $count, 'pageSize' => '1']);
		$articles = $query->offset($page->offset)
		    ->limit($page->limit)
		    ->all();
		    //var_dump($articles);


        $childcontent = Articles::find()->where(['status' => 1, 'category_id'=>$categoryid])->asArray()->all();

		$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
        return $this->render('committee',[
            'child' => $child,
            'childcontent' => $articles,
            'page' => $page,
        ]);
   	}

	public function actionChapter(){
	 	$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = $this->action->id;
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}
        $info = Articles::find()->where(['category_id'=>$categoryid])->asArray()->one();

		$title = $info['title'];
		$subhead = $info['subhead'];
		$intro = $info['intro'];
		$img = $info['img'];
		$link = $info['link'];
		$content = $info['content'];
		$seokeyword = $info['seokeyword'];
		$seocontent = $info['seocontent'];

		$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
        return $this->render('jianjie',[
            'title' => $title,
            'subhead' => $subhead,
            'intro' => $intro,
            'img' => $img,
            'link' => $link,
			'content' => $content,
            'seokeyword' => $seokeyword,
            'seocontent' => $seocontent,
            'child' => $child,
        ]);
   	}







	//文章列表
	public function actionList(){
		$member_id = Yii::$app->user->id;
		$data = ArtForm::find()->where(['status'=>10,'member_id'=>$member_id])->all();  
        return $this->render('list', [  
            'art' => $data,        
        ]);  			
	}
	
	//在文章列表修改文章
	public function actionUpdate($id){
		$data = ArtForm::find()->where(['id'=>$id])->asArray()->one();	
		return $this->render('update', [  
            'art' => $data,        
        ]); 	  			
	}
	public function actionUpdatedata(){
			$model= ArtForm::findOne(Yii::$app->request->post()['id']);
			$model->title = Yii::$app->request->post()['title'];
			$model->intro = Yii::$app->request->post()['intro'];
			$model->img = Yii::$app->request->post()['file-upload'];
			$model->link = Yii::$app->request->post()['link'];
			$model->content = Yii::$app->request->post()['w0'];
			$model->seokeyword = Yii::$app->request->post()['seokeyword'];
			$model->seocontent = Yii::$app->request->post()['seocontent'];
			if($model->save(false)){
				return $this->redirect(['art/list']);
			}else{
				return $this->goback();
			}	
	}
	//放入回收站
 	public function actionBin($id){
		$model= ArtForm::findOne($id);	
			$model->status = 5;
			if($model->save(false)){
				return $this->redirect(['art/list']);
			}else{
				return $this->goback();
			}
	}
	//恢复文件
	public function actionSbin($id){
		$model= ArtForm::findOne($id);	
			$model->status = 10;
			if($model->save(false)){
				return $this->redirect(['art/bins']);
			}else{
				return $this->goback();
			}
	}
	//回收站
	public function actionBins(){
		$member_id = Yii::$app->user->id;
		$data = ArtForm::find()->where(['status'=>5, 'member_id'=>$member_id])->all();  
        return $this->render('bins', [  
            'art' => $data,        
        ]);  			
	}
	//在回收站修改文章
	public function actionBinupdate($id){
		$data = ArtForm::find()->where(['id'=>$id])->asArray()->one();	
		return $this->render('binupdate', [  
            'art' => $data,        
        ]); 	  			
	}
	public function actionUpdatebin(){
			$model= ArtForm::findOne(Yii::$app->request->post()['id']);
			$model->title = Yii::$app->request->post()['title'];
			$model->intro = Yii::$app->request->post()['intro'];
			$model->img = Yii::$app->request->post()['file-upload'];
			$model->link = Yii::$app->request->post()['link'];
			$model->content = Yii::$app->request->post()['w0'];
			$model->seokeyword = Yii::$app->request->post()['seokeyword'];
			$model->seocontent = Yii::$app->request->post()['seocontent'];
			if($model->save(false)){
				
				return $this->redirect(['art/bins']);
			}else{
				return $this->goback();
			}	
	}
	//在回收站中删除文件
	public function actionBindelete($id){  
		   $model = ArtForm::findOne($id)->delete(); 
           if($model){
				return $this->redirect(['art/bins']);
			}else{
				return $this->goback();
			}	
	}
    	

   

 

}